<?php
//占位
class class_webpool
{
	var $vs = [];
	
	//---------------------------------------------------------------------------------------
	function on()
	{
		
	}
	//---------------------------------------------------------------------------------------
	function off()
	{

	}
//--------------------------------------------------------------------------------
	function get($type, $int)
	{
		$func = "init_{$type}";
		$this->{$func}();
		$object = $this->vs[$type][$int];
		return $object;
	}
//--------------------------------------------------------------------------------
	function init_mysql()
	{
		foreach($GLOBALS['MYSQL_CONFIG'] as $k => $v)
		{
			if(empty($this->vs['mysql']) || empty($this->vs['mysql'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 3306;
				$user = $v['user'];
				$user = $user ? $user : '';
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$dbname = $v['dbname'];
				
				try
				{
					$dsn = "mysql:dbname={$dbname};host={$host};port={$port}";
					$conn = NEW PDO($dsn, $user, $pwd);
				}
				catch(Exception $e)
				{
					_log1('mysql conn error');
				}
				
				if(empty($conn))
				{
					_log1('mysql conn error');
					return null;
				}
				
				$this->vs['mysql'][$k] = $conn;
			}
		}
	}
//--------------------------------------------------------------------------------

	function init_redis()
	{
		foreach($GLOBALS['REDIS_CONFIG'] as $k => $v)
		{
			if(empty($this->vs['redis']) || empty($this->vs['redis'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 6379;
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$index = $v['index'];
				
				$redis = NEW Redis();
				$redis->pconnect($host, $port ? $port : 6379);
				
				$stat = $redis->PING();
				if(!$stat)
				{
					_log1('redis conn error');
					return null;
				}
				
				$stat = true;
				
				if($pwd)
					$stat = $redis->AUTH($pwd);
				
				if(!$stat)
					return null;
				
				$redis->SELECT($index);

				$this->vs['redis'][$k] = $redis;
			}
		}
	}
//--------------------------------------------------------------------------------

	function init_mongo()
	{		
		foreach($GLOBALS['MONGO_CONFIG'] as $k => $v)
		{			
			if(empty($this->vs['mongo']) || empty($this->vs['mongo'][$k]))
			{
				$host = $v['host'];
				$port = $v['port'];
				$port = $port ? $port : 27017;
				$user = $v['user'];
				$user = $user ? $user : '';
				$pwd = $v['pwd'];
				$pwd = $pwd ? $pwd : '';
				$dbname = $v['dbname'];
				
				$str = '';
				if($user && $pwd)
				{
					$str = "{$user}:{$pwd}@";
				}
				
				$dns = "mongodb://{$str}{$host}:{$port}";		
		    $conn = new MongoDB\Driver\Manager($dns);		    
				$this->vs['mongo'][$k] = [$conn,$dbname];
			}
		}
	}
}